#******************************************************************
#
# Copyright 2016 Samsung Electronics All Rights Reserved.
#
#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

######################################################################
# easy-setup Mediator C++[Rich] SDK build script
######################################################################
import os.path
Import('env')

easysetup_path = os.curdir

# Add third party libraries
lib_env = env.Clone()
SConscript('#service/third_party_libs.scons', 'lib_env')

easy_setup_env = lib_env.Clone()

if easy_setup_env.get('RELEASE'):
    easy_setup_env.AppendUnique(CCFLAGS=['-Os'])
else:
    easy_setup_env.AppendUnique(CCFLAGS=['-g'])

target_enrollee = env.get('ES_TARGET_ENROLLEE')
target_os = env.get('TARGET_OS')

######################################################################
# Build flags
######################################################################
easy_setup_env.AppendUnique(CPPPATH=[
    'inc',
    'src',
    '../../inc',
    '#/resource/c_common',
    '#/resource/c_common/oic_malloc/include',
    '#/resource/c_common/oic_string/include',
    '#/resource/include',
    '#/resource/csdk/include',
    '#/resource/csdk/stack/include',
    '#/resource/oc_logger/include'
])

if target_os not in ['windows']:
    easy_setup_env.AppendUnique(CXXFLAGS=['-Wall', '-std=c++0x'])

if target_os in ['linux', 'tizen']:
    easy_setup_env.AppendUnique(LIBS=['dl', 'coap'])

easy_setup_env.AddPthreadIfNeeded()

if target_os in ['android']:
    if env.get('SECURED') == '1':
        easy_setup_env.AppendUnique(LIBPATH=[
            env.get('SRC_DIR') +
            '/java/iotivity-android/build/native-libs/armeabi'
        ])
    easy_setup_env.AppendUnique(CXXFLAGS=['-frtti', '-fexceptions'])
    easy_setup_env.PrependUnique(LIBS=[
        'oc',
        'octbstack',
        'oc_logger',
        'connectivity_abstraction',
        'gnustl_shared'
    ])
    if env.get('SECURED') == '1':
        easy_setup_env.AppendUnique(LIBS=['ocpmapi', 'ocprovision'])
    if not env.get('RELEASE'):
        easy_setup_env.AppendUnique(LIBS=['log'])

if target_os in ['linux', 'tizen']:
    easy_setup_env.AppendUnique(CXXFLAGS=['-pthread'])
    easy_setup_env.PrependUnique(LIBS=[
        'oc',
        'octbstack',
        'oc_logger',
    ])
    if env.get('SECURED') == '1':
        easy_setup_env.AppendUnique(LIBS=['ocpmapi', 'ocprovision'])

if target_os in ['android', 'ios', 'linux', 'tizen']:
    easy_setup_env.PrependUnique(CPPPATH=[
        env.get('SRC_DIR') + '/resource/c_common/oic_malloc/include',
        env.get('SRC_DIR') + '/resource/include',
        env.get('SRC_DIR') + '/resource/csdk/logger/include',
        env.get('SRC_DIR') + '/resource/csdk/include',
        env.get('SRC_DIR') + '/resource/csdk/stack/include',
        env.get('SRC_DIR') + '/resource/csdk/logger/include',
        env.get('SRC_DIR') + '/resource/csdk/security/include',
        env.get('SRC_DIR') + '/extlibs/cjson',
        env.get('SRC_DIR') + '/service/easy-setup/inc'
    ])
    if target_os not in ['linux', 'tizen']:
        easy_setup_env.PrependUnique(
            CPPPATH=[env.get('SRC_DIR') + '/extlibs/sqlite3'])
    if env.get('SECURED') == '1':
        with_upstream_libcoap = easy_setup_env.get('WITH_UPSTREAM_LIBCOAP')
        if with_upstream_libcoap == '1':
            easy_setup_env.AppendUnique(
                CPPPATH=['#extlibs/libcoap/libcoap/include'])
        else:
            easy_setup_env.AppendUnique(CPPPATH=[
                env.get('SRC_DIR') +
                '/resource/csdk/connectivity/lib/libcoap-4.1.1/include'
            ])
        easy_setup_env.AppendUnique(CPPPATH=[
            env.get('SRC_DIR') + '/resource/csdk/security/provisioning/include',
            env.get('SRC_DIR') + '/resource/csdk/security/include/internal',
            env.get('SRC_DIR') + '/resource/csdk/connectivity/api/',
            env.get('SRC_DIR') + '/resource/csdk/security/provisioning/include/internal',
            env.get('SRC_DIR') + '/resource/csdk/security/provisioning/include/cloud',
            env.get('SRC_DIR') + '/resource/csdk/security/provisioning/include/oxm'
        ])
        easy_setup_env.AppendUnique(LIBS=['mbedtls', 'mbedx509', 'mbedcrypto'])

######################################################################
# Source files and Targets
######################################################################

print("easysetup_path %s" % easysetup_path)

es_src = None

if target_os in ['android', 'ios', 'linux', 'tizen']:
    if env.get('SECURED') == '1':
        easy_setup_env.AppendUnique(
            es_src=[os.path.join(easysetup_path, 'src/EnrolleeSecurity.cpp')])

    es_common_src = [
        'src/RemoteEnrollee.cpp',
        'src/EnrolleeResource.cpp',
        'src/CloudResource.cpp',
        'src/EasySetup.cpp',
        'src/ESException.cpp',
    ]

    easy_setup_env.AppendUnique(es_src=es_common_src)

    es_sdk_static = easy_setup_env.StaticLibrary('ESMediatorRich',
                                                 easy_setup_env.get('es_src'))
    easy_setup_env.InstallTarget(es_sdk_static, 'libESMediatorRich')
    if target_os not in ['ios']:
        es_sdk_shared = easy_setup_env.SharedLibrary('ESMediatorRich',
                                                     easy_setup_env.get('es_src'))
        easy_setup_env.InstallTarget(es_sdk_shared, 'libESMediatorRich')
        easy_setup_env.UserInstallTargetLib(es_sdk_shared, 'libESMediatorRich')
        easy_setup_env.AppendUnique(
            LINKFLAGS=['-Wl,-soname,libESMediatorRich.so'])

filenames = [
    'ESException.h',
    'ESRichCommon.h',
    'EasySetup.hpp',
    'RemoteEnrollee.h'
]
for filename in filenames:
    easy_setup_env.UserInstallTargetHeader(
        'inc/' + filename, "service/easy-setup", filename)

print("Files path is %s" % env.get('es_src'))

######################################################################
# Generate RichSDK Mediator AAR
################################################ ######################

if target_os in ['android']:
    SConscript('android/SConscript')

######################################################################
# Build RichSDK Mediator Sample App
################################################ ######################
if target_os in ['linux']:
    SConscript('../../sampleapp/mediator/linux/richsdk_sample/SConscript')

 
if target_os in ['android']:
    SConscript('../../sampleapp/mediator/android/SConscript')

######################################################################
#Build UnitTestcases for Mediator[RichSDK]
################################################ ######################
if easy_setup_env.get('WITH_TEST') and easy_setup_env.get('SECURED') == '0':
    if target_os == 'linux':
        SConscript('unittests/SConscript')
